<?php

namespace Users\Register\Helper;

use Pinenut\User\Profile;
use Pinenut\User\Auth\EmailPassword as Auth;
use Pinenut\Form;
use Users\Register\EmailPasswordFormConfig as FormConfig;
/**
 * Обработчик формы локальной регистрации
 *
 * @author kosh
 *
 */
trait EmailPassword {
	function auth() {
		$formConfig = FormConfig::get();
		$formConfig['htmlOptions']['action'] = $this->url('users.register.emailPassword');

		$form = new Form ( $this->request->getSessionId (), 'emailPasswordRegister', $formConfig );

		if ($this->request->isPost() && $this->request->isAjax()) {
			$form->process($this->request->params(), true);
			$this->response->content =  $form->render();
			$this->response->noRender(true);
			return;

		} else if ($this->request->isPost ()) {
			if (!$form->process($this->request->params())) {
				$this->response->form = $form;
				return;
			}
			// уже проверили что всё ок
			// создаём пользователя
			$data = $form->getValues();
			$auth = new Auth();
			$auth->email = $data['email'];
			$auth->setPassword($data['password']);
			// сохраняем данные, чтобы использовать их при заполнении профиля
			$this->data = $data;
			$form->flush();

			return $auth;
		}
		$this->response->form = $form;
	}
	function profile() {
		$profile = new Profile();
		$profile->nickname = $this->data['name'];
		return $profile;
	}
}